import 'dart:async';

import 'package:flutter/material.dart';
import 'package:flutter_house_manager/api/home.dart';
import 'package:flutter_house_manager/pages/Home/components/home_list.dart';
import 'package:flutter_house_manager/pages/Home/components/home_nav.dart';
import 'package:flutter_house_manager/utils/TokenManager.dart';
import 'package:flutter_house_manager/utils/toast.dart';

class HomePage extends StatefulWidget {
  const HomePage({super.key});

  @override
  _HomePageState createState() => _HomePageState();
}

class _HomePageState extends State<HomePage> {
  List _announceList = [];
  int _count = 60;
  Timer? _timer;
  @override
  void initState() {
    // TODO: implement initState
    getAnnounceList();
    super.initState();
  }

  /// 获取公告列表
  getAnnounceList() async {
    try {
      // 请求数据
      var res = await getAnnounceListAPI();
      _announceList = res;
      setState(() {});
      PromptAction.showSuccess('获取数据成功');
    } catch (e) {
      PromptAction.showError('获取数据失败');
    }
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        appBar: AppBar(
          centerTitle: true,
          title: const Text('享+社区'),
        ),
        body: ListView(
          children: [
            const HomeNav(),
            Image.asset('assets/images/banner@2x.jpg'),
            HomeList(
              list: _announceList,
            ),
          ],
        ));
  }

  testToken() {
    return Row(
      children: [
        ElevatedButton(
            onPressed: () {
              tokenManager.setToken('123456787');
            },
            child: const Text('测试token')),
        ElevatedButton(
            onPressed: () async {
              final token = tokenManager.getToken();
              PromptAction.showSuccess('当前token为:$token');
            },
            child: const Text('查询token')),
        ElevatedButton(
            onPressed: () async {
              await tokenManager.removeToken();
              PromptAction.showSuccess('删除token成功');
            },
            child: const Text('删除token'))
      ],
    );
  }

  testTIME() {
    return Row(
      children: [
        ElevatedButton(
            onPressed: () {
              if (_timer != null) {
                return;
              }
              _timer = Timer.periodic(const Duration(seconds: 1), (timer) {
                setState(() {
                  _count--;
                });
                if (_count <= 0) {
                  _timer?.cancel();
                  _timer = null;
                  _count = 60;
                }
              });
            },
            child: const Text('开始')),
        Text('$_count'),
        ElevatedButton(
            onPressed: () {
              if (_timer == null) {
                return;
              }
              _timer?.cancel();
              _timer = null;
              _count = 60;
              setState(() {});
            },
            child: const Text('结束'))
      ],
    );
  }
}
